//
//  HCallCenterViewController.swift
//  UserApp_iOS
//
//  Created by BestVast on 2021/6/3.
//  Copyright © 2021 linlin. All rights reserved.
//

import UIKit

/// 客服中心页面
class HCallCenterViewController: BaseViewController {
    var scrollView: UIScrollView!
    var topView: UIView!
    var bottomView: UIView!
    var models: [String] = [] {
        didSet {
            self.uiConfig()

            if models.count > 0 {
                
                addBottomView()
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    

    func uiConfig() -> Void {
        self.view.backgroundColor = colorThemeBackground
        self.navigationItem.title = "客服中心"
        scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 88, width: screenWidth, height: screenHeight - 88 - 66))
        scrollView.contentSize = CGSize.init(width: screenWidth, height: screenHeight - 88 - 65)
        self.view.addSubview(scrollView)
        self.addTopView()
        self.addMiddleView()
//        self.addBottomView()
    }
    func addTopView() -> Void {
        topView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: screenWidth - 20, height: 120))
        topView.backgroundColor = .white
        topView.layer.cornerRadius = 10
        topView.layer.masksToBounds = true
        scrollView.addSubview(topView)
        let consultLabel = UILabel.init(frame: CGRect.init(x: 15, y: 10, width: 200, height: 20))
        consultLabel.text = "请选择您想要的咨询方式"
        consultLabel.font = .systemFont(ofSize: 16)
        consultLabel.textColor = .gray
        topView.addSubview(consultLabel)
        
        let lineView = UIView.init(frame: CGRect.init(x: 0, y: 40, width: screenWidth, height: 1))
        lineView.backgroundColor = colorThemeBackground
        topView.addSubview(lineView)
        
        let btnWidth = (screenWidth - 100) / 2
        
        let leftButton = UIButton.init(type: .custom)
        leftButton.frame = CGRect.init(x: 20, y: 60, width: btnWidth, height: 40)
        leftButton.contentHorizontalAlignment = .center
        leftButton.backgroundColor = colorTheme
        leftButton.layer.cornerRadius = 5
        leftButton.layer.masksToBounds = true
        topView.addSubview(leftButton)
        
        leftButton.sg_setImage(location: .right, space: 0.0, completion: {
            (btn) in
            btn.setTitle("在线咨询", for: .normal)
            btn.setImage(UIImage.init(named: "btn_set_help_pre"), for: .normal)
        })

        let rightButton = UIButton.init(type: .custom)
        rightButton.frame = CGRect.init(x: screenWidth / 2 + 20, y: 60, width: btnWidth, height: 40)
        rightButton.contentHorizontalAlignment = .center
        rightButton.backgroundColor = .green
        rightButton.layer.cornerRadius = 5
        rightButton.layer.masksToBounds = true
        topView.addSubview(rightButton)
        
        rightButton.sg_setImage(location: .right, space: 0.0, completion: {
            (btn) in
            btn.setTitle("在线咨询", for: .normal)
            btn.setImage(UIImage.init(named: "btn_set_help_pre"), for: .normal)
        })
      
        
    }
    func addMiddleView() -> Void {
        let timeLablel = UILabel.init(frame: CGRect.init(x: (screenWidth - 245) / 2, y: topView.bottom + 20, width: 245, height: 20))
        timeLablel.backgroundColor = UIColor(red: 220/255.0, green: 220/255.0, blue: 220/255.0, alpha: 1)
        timeLablel.text = "客服工作时间：8：30 - 18：40"
        timeLablel.textColor = UIColor(red: 160/255.0, green: 160/255.0, blue: 160/255.0, alpha: 1)
        timeLablel.textAlignment = .center
        timeLablel.layer.cornerRadius = 10
        timeLablel.layer.masksToBounds = true
        timeLablel.font = .systemFont(ofSize: 14)
        scrollView.addSubview(timeLablel)
    }
    func addBottomView() -> Void {
        let heigth: CGFloat = CGFloat(41 + 41 * models.count - 1)
        bottomView = UIView.init(frame: CGRect.init(x: 10, y: 190, width: screenWidth - 20, height: heigth))
        bottomView.backgroundColor = .white
        bottomView.layer.cornerRadius = 10
        bottomView.layer.masksToBounds = true
        scrollView.addSubview(bottomView)
        let consultLabel = UILabel.init(frame: CGRect.init(x: 15, y: 10, width: 200, height: 20))
        consultLabel.text = "猜你想问"
        consultLabel.font = .systemFont(ofSize: 16)
        consultLabel.textColor = .gray
        bottomView.addSubview(consultLabel)
        
        let lineView = UIView.init(frame: CGRect.init(x: 0, y: 40, width: screenWidth, height: 1))
        lineView.backgroundColor = colorThemeBackground
        bottomView.addSubview(lineView)
        
        for index in 0..<models.count {
            let title = models[index]
            let questionView = QuestionView.init(frame: CGRect.init(x: 0, y: lineView.bottom + CGFloat(41 * index) + 10, width: screenWidth - 20, height: 20))
            bottomView.addSubview(questionView)
            questionView.titleLabel.text = title
            if index < models.count - 1 {
                let lineView = UIView.init(frame: CGRect.init(x: 0, y: lineView.bottom + CGFloat(41 * index) + 40, width: screenWidth, height: 1))
                lineView.backgroundColor = colorThemeBackground
                bottomView.addSubview(lineView)
            }
        }
    }

}

class QuestionView: UIView {
    var titleLabel: UILabel!
    var lookBtn: UIButton!
    @objc func clickLookBtn() {
        
    }
    func uiConfig() -> Void {
        titleLabel = UILabel.init(frame: CGRect.init(x: 15, y: (height - 20) / 2, width: width - 75 , height: 20))
        titleLabel.font = .systemFont(ofSize: 16)
        titleLabel.textColor = UIColor(red: 66/255.0, green: 66/255.0, blue: 66/255.0, alpha: 1)
        self.addSubview(titleLabel)
        
        lookBtn = UIButton.init(type: .custom)
        lookBtn.frame = CGRect.init(x: width - 60, y: (height - 20) / 2, width: 45, height: 20)
        lookBtn.setTitle("查看", for: .normal)
        lookBtn.titleLabel?.font = .systemFont(ofSize: 14)
        lookBtn.setTitleColor(colorTheme, for: .normal)
        self.addSubview(lookBtn)
        lookBtn.addTarget(self, action: #selector(clickLookBtn), for: .touchUpInside)

    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        uiConfig()
    }
}
